#!/usr/bin/env python
# coding: utf-8

# In[1]:


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# In[3]:


df = pd.read_csv('/Users/yanghongyi/Desktop/第一章：数据科学原理与数据处理【314120】数据科学原理与数据处理流程/模块四 第一章/28.代码/log.txt',header = None)
df.head()


# In[27]:


df = pd.read_csv('/Users/yanghongyi/Desktop/第一章：数据科学原理与数据处理【314120】数据科学原理与数据处理流程/模块四 第一章/28.代码/log.txt',header = None,sep='\t' )
df.head()


# In[28]:


df.columns = ['id','api','count','res_time_sum','res_time_min','res_time_max','res_time_avg','interval','created_at']
df.head(2)


# In[6]:


df.sample(5) #随机采样，多次执行


# In[7]:


df.shape


# In[8]:


df.dtypes


# In[9]:


df.info() # 磁盘占用情况


# In[10]:


df['api'].describe()


# In[11]:


df['created_at'].describe()


# In[13]:


df.index


# In[14]:


df.index = df['created_at']


# In[15]:


df.info()


# In[43]:


df.index = pd.to_datetime(df.created_at)


# In[44]:


df.index #变成时间类型索引 唯一性


# In[18]:


df['2019-05-01']


# In[19]:


df.api.unique()


# In[30]:


df.drop('id',axis = 1)
df.drop('api',axis = 1)
df.drop('interval',axis = 1)


# In[32]:


df = df.drop(['id','api','interval'],axis = 1)
df.head()


# In[33]:


df.info()


# In[34]:


df.describe()


# In[37]:


df['count'].hist(bins = 30)
plt.show() #大部分在十次以内


# In[45]:


# 切出一天的数据，绘制一天时段的借口调用情况
df['2019-5-1']['count'].plot()
plt.show()


# In[46]:


# 重采样
df2 = df['2019-5-1']


# In[48]:


df2 = df2[['count']].resample('1H').mean()


# In[59]:


plt.figure(figsize = (10,5))
df2['count'].plot()
plt.show()


# In[54]:


## 折线图和直方图可以看到业务的高峰时段
plt.figure(figsize = (10,5))
df2['count'].plot(kind = 'bar')
plt.xticks(rotation = 60)
plt.show()


# In[60]:


# 分析异常 使用箱线图
plt.figure(figsize = (10,6))
df['2019-5-1'][['count']].boxplot(showmeans = True,meanline = True)
plt.show()


# In[61]:


df[df['count']>20]


# In[64]:


# 分析某一天的响应时间
plt.figure(figsize = (10,5))
df['2019-5-5']['res_time_avg'].plot()
plt.show()


# In[66]:


df2 = df['2019-5-1']
df2[df['res_time_avg']>1000]


# In[70]:


plt.figure(figsize = (10,6))
data = df['2019-5-1'].resample('20T').mean()
data[['res_time_sum','res_time_min','res_time_max','res_time_avg']].plot()
plt.show()


# In[71]:


df['2019-5-1':'2019-5-10']['count'].plot()
plt.show()


# In[76]:


# 周末与平时
df['2019-5-1'].index.weekday
df['weekday'] = df.index.weekday
df.head()


# In[79]:


df['weekend'] = df['weekday'].isin({5,6})
df.head()


# In[81]:


df.groupby('weekend')['count'].mean()


# In[83]:


df.groupby(['weekend',df.index.hour])['count'].mean()


# In[84]:


df.groupby(['weekend',df.index.hour])['count'].mean().plot()
plt.show()


# In[87]:


df.groupby(['weekend',df.index.hour])['count'].mean().unstack(level = 0)


# In[88]:


df.groupby(['weekend',df.index.hour])['count'].mean().unstack(level = 0).plot()
plt.show()


# In[ ]:




